/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package estadodpuertoar;

//import com.sun.org.apache.xml.internal.serializer.utils.StringToIntTable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 *
 * @author jonathan
 */
public class NetstatJava {
    
    String [] A = new String[4];
    Object [][] Data = new Object[5][5];

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        NetstatJava ns = new NetstatJava();
        
        final String cmd = "netstat"; 

        try {
            Process process = Runtime.getRuntime().exec(cmd);
            
            BufferedReader  stdInput = new BufferedReader(new InputStreamReader(
                    process.getInputStream(),"iso-8859-1"));
            
//            BufferedReader stdError = new BufferedReader(new InputStreamReader(
//                    process.getErrorStream()));
            
            StringBuilder copy = new StringBuilder();
            
//            Object [][]Data = new Object[10][4];;
            
            copy.append(ns.getText(stdInput));
            ns.getArr(copy);
//            System.out.println(copy);
            
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
    
    public StringBuilder getText(BufferedReader stdInput) throws IOException{
        StringBuilder sb = new StringBuilder();
        String s, cont, aux,buff;
        int i=0, c=0, cl=0;
        
        while((s = stdInput.readLine())!= null){             
                cont=s.trim();
                if (cont.startsWith("Proto")) {
                    StringTokenizer token = new StringTokenizer(cont);
                    A = new String[token.countTokens()];
                    while (token.hasMoreTokens()) {    
                        buff = token.nextToken();
                        if (!buff.equals("Direcci¢n")) {
                            A[i]= buff;
                        } else {
                            c++;
                            A[i]= buff + " " + token.nextToken();
                        }
                        i++;
                    }
                }
                
                if (!cont.equals("") & !cont.startsWith("Conexiones") & !cont.startsWith("Proto")) {
                    cl++;
                    StringTokenizer tD = new StringTokenizer(cont);
                    while(tD.hasMoreTokens()){
                        sb.append(tD.nextToken()).append(" ");
                    }
                    sb.append("\n");
                }
            }
        
//            while((s = stdError.readLine())!= null){
//                System.out.println(s);
//            }
        
        return sb;
    }
    
    public void getArr(StringBuilder copy){
        int p=0, a=0;
        String line="";
        p = copy.indexOf("\n");
        line=copy.substring(0,p);
        while(copy.length()!=p){
            line=copy.substring(a,p);
            System.out.println(line);
            a=p+1;
            p = copy.indexOf("\n",p+1);
        }
    }
}
